//cart.js

require('page/common/header/index.js')
var _account_service = require('service/account-service.js')
var _cart_service = require('service/cart-service.js')
var _catalog_service = require('service/catalog-service.js')
var _util = require('util/util.js');
var ListTempalte = require('./index.string');

var cart = {
    cartData: {
        subTotal: '',
        cartList: {}
    },

    init: function () {
        this.loadCartInfo();
        this.bindEvents();
        return this;
    },

    bindEvents: function () {
        var _this = this;
        var quantity = '';
        var Bquantity = '';
        var TworkingItemId = '';

        $(document).delegate('#vieworderForm', 'click', function () {
            if (_this.cartData.cartList.length === 0) {
                alert("Your cart is empty.")
                return
            }
            window.location.href = './order-orderForm.html';
        });

        $(document).delegate('.remove-btn', 'click', function () {
            var workingItemId = this.id;
            // console.log('workingItemId:' + workingItemId);
            _this.removeItem(workingItemId);
        });

        $(document).delegate('.quantity', 'focus', function () {
            TworkingItemId = this.id;
            Bquantity = $('#' + TworkingItemId).val();
            console.log('获得焦点');
        });

        $(document).delegate('.quantity', 'blur', function () {
            console.log('失去焦点');

            var str = new RegExp('qty_');
            var workingItemId = TworkingItemId.replace(str, '');
            quantity = $('#' + TworkingItemId).val();

            if ((/^(\+|-)?\d+$/.test(quantity)) && quantity > 0) {
                _this.updateItem(workingItemId, quantity);
            } else {
                $('.quantity').html(Bquantity);
                alert('请输入正整数');
                window.location.reload();
            }
        });

        $(document).keydown(function (e) {
            if (e.keyCode == 13) {
                console.log('回车');

                var str = new RegExp('qty_');
                var workingItemId = TworkingItemId.replace(str, '');
                quantity = $('#' + TworkingItemId).val();

                if ((/^(\+|-)?\d+$/.test(quantity)) && quantity > 0) {
                    _this.updateItem(workingItemId, quantity);
                } else {
                    $('.quantity').html(Bquantity);
                    alert('请输入正整数');
                    window.location.reload();
                }

                // _this.updateItem(workingItemId, quantity);
            }
        })
    },

    loadCartInfo: function () {
        var _this = this;


        _this.initCart();


        // _account_service.checkLogin(
        //     //成功调用
        //     function (res) {

        //     },
        //     //失败调用
        //     function (errMsg) {
        //         console.log(errMsg);
        //     }
        // )
    },
    //刷新界面
    initCart: function () {
        var _this = this;

        _cart_service.getCart(
            function (res) {
                console.log(res);
                _this.cartData.cartList = res.itemList;
                _this.cartData.subTotal = res.subTotal;

                var result = _util.renderHtml(ListTempalte, { CartData: _this.cartData });
                $('#Cart').html(result);

                if (_this.cartData.cartList.length === 0) {
                    $('#emptyInfo').show();
                    $(document).delegate('.Button', 'click', function () {
                        alert('购物车为空，无法生成订单');
                    });
                }
                else {
                    $('#emptyInfo').hide();
                }
            },
            function (errMsg) {
                console.log('getCartByUsernameerrMsg: ' + errMsg);
            }
        )



    },

    insertItem: function (workingItemId) {
        var _this = this;
        _cart_service.insertItem(workingItemId,
            function (res) {
                console.log('insertItem');
                // _this.getCartByUsername();
            },
            function (errMsg) {
                console.log('insertItemerrMsg: ' + errMsg);
            }
        )

    },

    removeItem: function (workingItemId) {
        var _this = this;

        _cart_service.removeItem(workingItemId,
            function (res) {
                console.log('removeItem');
                _this.initCart();
            },
            function (errMsg) {
                console.log('removeItemerrMsg: ' + errMsg);
            }
        )
    },

    updateItem: function (workingItemId, quantity) {
        var _this = this;

        _cart_service.updateItem(workingItemId, quantity,
            function (res) {
                console.log('updateItem');
                _this.initCart();
            },
            function (errMsg) {
                console.log('updateItemerrMsg: ' + errMsg);
            }
        )
    },
}

module.exports = cart.init();